Ext.namespace('SLM');

SLM.EventsMatrix = function(config) {
	this.store = new Ext.data.JsonStore({
			url: Config.baseUrl + 'resources/api/app.php'
		,	baseParams: { cmd: 'checklist-event'
				,	values: Config.eventFields
			}
		,	sortInfo:{field: 'event_type', direction: "ASC"}			
		,	remoteSort: true		
		,	root: 'records'
		,	total: 'totalCount'
		,	fields: [
					'title'
				,	'event_type_id'
				, 'R0'
				, 'R1'
				, 'R2'
				, 'R3'
				, 'R4'
				, 'R5'
				, 'R6'
				, 'R7'
				, 'R8'
				, 'R9'
				, 'R10'
				, 'R11'
				, 'R12'
				, 'R13'
				, 'R14'
				, 'R15'
				, 'R16'
				, 'R17'
				, 'R18'
				, 'R19'
				, 'R20'
				, 'R21'
				, 'R22'
				, 'R23'
				, 'R24'
				, 'R25'
				, 'R26'
				, 'R27'
				, 'R28'
				, 'R29'
				, 'R30'
			]
		,	listeners: {			
				'beforeload': function( store, o ) {
						var filters = Ext.getCmp('event_form').getForm().getValues( true );

						var filter = {
								condition: 'AND'
							,	items: []
						}
						
						var found = false;
						var condition = null;
						Ext.each( filters.split("&"), function( tmpField, index ) {
							tmpField = tmpField.split("=");
							if (tmpField[1] != '') {
								found = true;
								switch( tmpField[0] ) {
									case 'date_start':
										condition = 'GREATER_EQUAL';
										break;
									case 'date_end':
										condition = 'LESS_EQUAL';
										break;
									default:
										condition = 'EQUAL';
										break;
								}
								filter.items.push({
										field: tmpField[0]
									,	condition: condition
									,	value: tmpField[1]
								});
							}
						});
						
						if (!found) {
							filter = null;
						}
						
						var params = {
								userFilter: Ext.getCmp('my_events').pressed
							,	filter: Ext.encode(filter)
						}
						Ext.apply(o.params, params );
				}
			}				
	});

	this.filters = new Ext.ux.grid.GridFilters({
			encode: true
		,	filters: [{
					type: 'string'
				,	dataIndex: 'title'
			}]
	});

  var pagingBar = new Ext.PagingToolbar({
			pageSize: 100
		,	store: this.store
		,	displayInfo: true
		,	displayMsg: 'Displaying events {0} - {1} of {2}'
		,	emptyMsg: "No events to display"
	});

	Ext.apply(this, config, {
			id: 'events-matrix'
		,	store: this.store
		,	iconCls: 'icon_events_matrix'
		,	title: 'Events (Matrix View)'
		,	columns: Config.eventMatrixColumns || []
		,	border: false
		, loadMask: true
		,	viewConfig: {
				emptyText: 'No events to display.'
			}
		,	sm: new Ext.grid.RowSelectionModel({singleSelect: true})
		,	bbar: pagingBar
	});

	SLM.EventsMatrix.superclass.constructor.apply(this, arguments);

};

Ext.extend( SLM.EventsMatrix, Ext.grid.GridPanel, {

		addEvent: function() {
			
			var addEvent = new SLM.Event({'cmd':'add'});
			addEvent.on('saveSuccessful', function() {
				this.getStore.reload();	
			});
			addEvent.show();
		}
		
	,	mySpecies: function( btn, toggle ) {
			this.store.load({ params: { userFilter: true } });
		}
				
});

Ext.reg('EventsMatrix', SLM.EventsMatrix);